import Vue from 'vue'
import store from '../store/index'
import VueRouter from 'vue-router'
import routes from './routes'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const router = new VueRouter({
  // mode: "history",
  mode: 'hash',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = store.state.token ? store.state.token : sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
